/**
 * 服务实例管理初始化
 */
var AppInstance = {
    id: "AppInstanceTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    catalogId: 0
};

/**
 * 初始化表格的列
 */
AppInstance.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '主键id', field: 'id', visible: false, align: 'center', valign: 'middle', sortable: true},
        {title: '目录id', field: 'catalogId', visible: false, align: 'center', valign: 'middle', sortable: true},
        {title: '服务图标', field: 'appIcon', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	var path = Sys.ctxPath+"/file/image/"+value;
            return "<img class='appIcon' src='"+path+"'/>";
        }},
        {title: '服务名称', field: 'appName', align: 'center', valign: 'middle', sortable: true},
        {title: '服务编码', field: 'appCode', align: 'center', valign: 'middle', sortable: true},
        {title: '服务类型', field: 'appTypeName', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(value=='本地服务'){
                return "<span class='label label-success'>本地服务</span>";
        	}else{
                return "<span class='label label-info'>远程服务</span>";
        	}
        }},
        {title: '服务状态', field: 'appStateName', align: 'center', valign: 'middle', sortable: true,formatter:function(value,row,index){
        	if(value=='启用'){
                return "<span class='label label-success'>启用</span>";
        	}else{
                return "<span class='label label-warning'>禁用</span>";
        	}
        }},
        {title: '描述', field: 'appDesc', align: 'center', valign: 'middle', sortable: true},
        {title: '排序', field: 'weight', align: 'center', valign: 'middle', sortable: true},
        {title: '创建时间', field: 'createDate', align: 'center', valign: 'middle', sortable: true},
    ];
};

/**
 * 检查是否选中
 */
AppInstance.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        AppInstance.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加服务实例
 */
AppInstance.openAddAppInstance = function () {
    var index = layer.open({
        type: 2,
        title: '添加服务实例',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Sys.backendPath + '/appinstance/to_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看服务实例详情
 */
AppInstance.openAppInstanceDetail = function () {
    if (this.check()) {
	    Sys.addTabPage('【'+AppInstance.seItem.appName+'】',Sys.backendPath + '/appinstance/to_update/' + AppInstance.seItem.id);
    }
};

/**
 * 删除服务实例
 */
AppInstance.del = function () {
    if (this.check()) {
        var operation = function(){
	        var ajax = new $ax(Sys.backendPath + "/appinstance/delete", function (data) {
	            Sys.success("删除成功!");
	            AppInstance.table.refresh();
	        }, function (data) {
	            Sys.error("删除失败!" + data.responseJSON.message + "!");
	        });
	        ajax.set("appInstanceId",AppInstance.seItem.id);
	        ajax.start();
	    };
	    Sys.confirm("是否删除服务实例?",operation);
    }
};

/**
 * 点击管理服务接口
 */
AppInstance.openAppResource = function () {
    if (this.check()) {
	    Sys.addTabPage('【'+AppInstance.seItem.appName+'】-服务接口',Sys.backendPath + '/appresource/'+AppInstance.seItem.id);
    }
};

/**
 * 点击管理服务接口文档
 */
AppInstance.openAppApi = function () {
    if (this.check()) {
	    Sys.addTabPage('【'+AppInstance.seItem.appName+'】-接口文档',Sys.backendPath + '/api/doc/'+AppInstance.seItem.appCode);
    }
};

/**
 * 查询服务实例列表
 */
AppInstance.search = function () {
    var queryData = {};
    queryData['catalogId'] = AppInstance.catalogId;
    queryData['appType'] = $("#appType").val();
    queryData['appCode'] = $("#appCode").val();
    queryData['appName'] = $("#appName").val();
    queryData['appState'] = $("#appState").val();
    queryData['appDesc'] = $("#appDesc").val();
    AppInstance.table.refresh({query: queryData});
};

AppInstance.onClickAppCatalog = function (e, treeId, treeNode) {
	AppInstance.catalogId = treeNode.id;
	AppInstance.search();
};
$(function () {
    var defaultColunms = AppInstance.initColumn();
    var table = new BSTable(AppInstance.id, Sys.backendPath + "/appinstance/list", defaultColunms);
    table.setPaginationType("server");
    table.setSortName("weight");
    AppInstance.table = table.init();
    var ztree = new $ZTree("catalogTree", Sys.backendPath + "/appcatalog/tree");
    ztree.bindOnClick(AppInstance.onClickAppCatalog);
    ztree.init();
});
